package com.demo.crm.springboot3security.exception;

import lombok.Getter;

/**
 * 表单关系异常基类
 */
public class FormRelationException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    @Getter
    private final String errorCode;

    public FormRelationException(String message, String errorCode) {
        super(message);
        this.errorCode = errorCode;
    }

    public FormRelationException(String message, String errorCode, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
    }

    /**
     * 表单关系未找到异常
     */
    public static class RelationNotFoundException extends FormRelationException {
        private static final long serialVersionUID = 1L;
        private static final String ERROR_CODE = "RELATION_NOT_FOUND";

        public RelationNotFoundException(String message) {
            super(message, ERROR_CODE);
        }

        public RelationNotFoundException(Long relationId) {
            super(String.format("未找到ID为%d的表单关系", relationId), ERROR_CODE);
        }
    }

    /**
     * 无效的表单关系异常
     */
    public static class InvalidRelationException extends FormRelationException {
        private static final long serialVersionUID = 1L;
        private static final String ERROR_CODE = "INVALID_RELATION";

        public InvalidRelationException(String message) {
            super(message, ERROR_CODE);
        }

        public static InvalidRelationException nullRelation() {
            return new InvalidRelationException("表单关系不能为空");
        }

        public static InvalidRelationException nullSourceFormId() {
            return new InvalidRelationException("源表单ID不能为空");
        }

        public static InvalidRelationException nullTargetFormId() {
            return new InvalidRelationException("目标表单ID不能为空");
        }

        public static InvalidRelationException nullRelationType() {
            return new InvalidRelationException("关系类型不能为空");
        }

        public static InvalidRelationException sourceFormNotFound(Long formId) {
            return new InvalidRelationException(String.format("源表单(ID=%d)不存在", formId));
        }

        public static InvalidRelationException targetFormNotFound(Long formId) {
            return new InvalidRelationException(String.format("目标表单(ID=%d)不存在", formId));
        }

        public static InvalidRelationException sourceFormNotPublished(Long formId) {
            return new InvalidRelationException(String.format("源表单(ID=%d)未发布，不能建立关系", formId));
        }

        public static InvalidRelationException targetFormNotPublished(Long formId) {
            return new InvalidRelationException(String.format("目标表单(ID=%d)未发布，不能建立关系", formId));
        }

        public static InvalidRelationException invalidRelationType(String type) {
            return new InvalidRelationException(String.format("无效的关系类型: %s", type));
        }

        public static InvalidRelationException sameSourceAndTarget() {
            return new InvalidRelationException("源表单和目标表单不能相同");
        }

        public static InvalidRelationException invalidJsonConfig() {
            return new InvalidRelationException("关系配置必须是有效的JSON格式");
        }

        public static InvalidRelationException batchSizeExceeded(int maxSize) {
            return new InvalidRelationException(String.format("批量操作数据不能超过%d条", maxSize));
        }
    }

    /**
     * 重复的表单关系异常
     */
    public static class DuplicateRelationException extends FormRelationException {
        private static final long serialVersionUID = 1L;
        private static final String ERROR_CODE = "DUPLICATE_RELATION";

        public DuplicateRelationException(String message) {
            super(message, ERROR_CODE);
        }

        public static DuplicateRelationException create(Long sourceFormId, Long targetFormId) {
            return new DuplicateRelationException(
                    String.format("源表单(ID=%d)和目标表单(ID=%d)之间已存在关系", sourceFormId, targetFormId));
        }
    }
}